package com.bankcomm.creditcard.redfriday.shop;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.animation.AnimationUtils;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;

import com.bankcomm.creditcard.redfriday.BaseActivity;
import com.bankcomm.creditcard.redfriday.R;

public class ShopTabActivity extends TabActivity {

	private TabHost mTabHost;
	private RadioGroup mRadioGroup;
	private int index;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shoptab);
		index = getIntent()
				.getIntExtra("index", BaseActivity.INDEX_SUPERMARKET);
		mTabHost = getTabHost();

		Intent intent = new Intent(getApplicationContext(),
				ShopListActivity.class);
		intent.putExtra("index", BaseActivity.INDEX_SUPERMARKET);
		mTabHost.addTab(mTabHost.newTabSpec("A").setIndicator("A")
				.setContent(intent));

		intent = new Intent(getApplicationContext(), ShopListActivity.class);
		intent.putExtra("index", BaseActivity.INDEX_GASSTATION);
		mTabHost.addTab(mTabHost.newTabSpec("B").setIndicator("B")
				.setContent(intent));

		intent = new Intent(getApplicationContext(), ShopListActivity.class);
		intent.putExtra("index", BaseActivity.INDEX_FIFTY);
		mTabHost.addTab(mTabHost.newTabSpec("C").setIndicator("C")
				.setContent(intent));
		mTabHost.setCurrentTab(index);

		mRadioGroup = (RadioGroup) findViewById(R.id.shoptab_radiogroup);
		RadioButton rb = (RadioButton) mRadioGroup.getChildAt(index);
		rb.setChecked(true);

		mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				int idx = -1;
				if (checkedId == R.id.shoptab_supermarket) {
					idx = 0;
				} else if (checkedId == R.id.shoptab_gasstation) {
					idx = 1;
				} else if (checkedId == R.id.shoptab_fifty) {
					idx = 2;
				}
				switchActivity(idx);
			}
		});
	}

	protected void switchActivity(int idx) {
		int n = mTabHost.getCurrentTab();
		if (idx < n) {
			mTabHost.getCurrentView().startAnimation(
					AnimationUtils.loadAnimation(this, R.anim.slide_left_out));
		} else if (idx > n) {
			mTabHost.getCurrentView().startAnimation(
					AnimationUtils.loadAnimation(this, R.anim.slide_right_out));
		}
		mTabHost.setCurrentTab(idx);
		if (idx < n) {
			mTabHost.getCurrentView().startAnimation(
					AnimationUtils.loadAnimation(this, R.anim.slide_left_in));
		} else if (idx > n) {
			mTabHost.getCurrentView().startAnimation(
					AnimationUtils.loadAnimation(this, R.anim.slide_right_in));
		}

		RadioButton rb = (RadioButton) mRadioGroup.getChildAt(idx);
		rb.setChecked(true);
	}
}